package menu;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

// 菜单栏里有菜单，菜单里有菜单项(普通选项，多选选项，单选选项)和子菜单
public class MenuBarDemo extends Application {
	private BorderPane root = new BorderPane();
	private MenuBar menuBar;
	
	public static void main(String[] args) {
	    launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		// 菜单条
	    menuBar = new MenuBar();  
	    menuBar.prefWidthProperty().bind(stage.widthProperty());
	    root.setTop(menuBar);
	
	    // File菜单
	    Menu fileMenu = new Menu("File");
	    
	    MenuItem open = new MenuItem("Open");
	    open.setDisable(true);                  // 禁用，点击无反应
	    
	    MenuItem save = new MenuItem("Save");
	    save.setStyle("-fx-background-color: purple");
	    
	    MenuItem exit = new MenuItem("Exit");
	    exit.setAccelerator(KeyCombination.valueOf("Ctrl+a"));   // 设置快捷键(相当于点击)
	    exit.setOnAction(e -> stage.close());   // 关闭窗口
	    
	    // 自定义选项(自定义结点)
	    CustomMenuItem cmi = new CustomMenuItem();
	    cmi.setContent(new Ellipse(15, 10));
	    fileMenu.getItems().addAll(open, save, new SeparatorMenuItem(), exit, cmi);    // exit和save之间增加一条分割线
	
	    // Web菜单
	    Menu webMenu = new Menu("Web");
	    
	    CheckMenuItem html = new CheckMenuItem("HTML");
	    html.setSelected(true);
	    
	    CheckMenuItem css = new CheckMenuItem("CSS");
	    css.setSelected(true);
	    webMenu.getItems().addAll(html, css);
	
	    // sql菜单
	    Menu sqlMenu = new Menu("SQL");
	    
	    ToggleGroup tGroup = new ToggleGroup();
	    RadioMenuItem mysql = new RadioMenuItem("MySQL");    // 单选选项
	    mysql.setToggleGroup(tGroup);
	
	    RadioMenuItem oracle = new RadioMenuItem("Oracle");
	    oracle.setToggleGroup(tGroup);
	    oracle.setSelected(true);
	    
	    // sql的子菜单
	    Menu tutorialMenu = new Menu("Tutorial");
	    tutorialMenu.getItems().addAll(
	        new CheckMenuItem("Java"),
	        new CheckMenuItem("JavaFX"),
	        new CheckMenuItem("Swing"));
	    sqlMenu.getItems().addAll(mysql, oracle, new SeparatorMenuItem(), tutorialMenu);
	    
	    // 放进菜单条
	    menuBar.getMenus().addAll(fileMenu, webMenu, sqlMenu);
	    
	    Scene scene = new Scene(root, 300, 250, Color.LAWNGREEN);
	    stage.setScene(scene);
	    stage.show();
	}
}